{janitor}包数据整理

packages
Author

Tony Duan

Published

October 4, 2022

变量选添删

Code
library(dplyr)
library(sqldf)
Code
glimpse(starwars_sample)
Rows: 5
Columns: 8
$ name       <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
$ height     <int> 172, 167, 96, 202, 150
$ mass       <dbl> 77, 75, 32, 136, 49
$ hair_color <chr> "blond", NA, NA, "none", "brown"
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light"
$ eye_color  <chr> "blue", "yellow", "red", "yellow", "brown"
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0
$ sex        <chr> "male", "none", "none", "male", "female"

变量选添删

Code
sqldf("select sex  from starwars_sample")
     sex
1   male
2   none
3   none
4   male
5 female
Code
starwars_sample %>% select(sex)
# A tibble: 5 × 1
  sex   
  <chr> 
1 male  
2 none  
3 none  
4 male  
5 female

Code
sqldf("select *,case when sex='male' then 1 else 0 end as male_flag from starwars_sample")
            name height mass hair_color  skin_color eye_color birth_year    sex
1 Luke Skywalker    172   77      blond        fair      blue       19.0   male
2          C-3PO    167   75       <NA>        gold    yellow      112.0   none
3          R2-D2     96   32       <NA> white, blue       red       33.0   none
4    Darth Vader    202  136       none       white    yellow       41.9   male
5    Leia Organa    150   49      brown       light     brown       19.0 female
  male_flag
1         1
2         0
3         0
4         1
5         0
Code
starwars_sample %>% mutate(male_flag=ifelse(sex=='male',1,0))
# A tibble: 5 × 9
  name   height  mass hair_color skin_color eye_color birth_year sex   male_flag
  <chr>   <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>     <dbl>
1 Luke …    172    77 blond      fair       blue            19   male          1
2 C-3PO     167    75 <NA>       gold       yellow         112   none          0
3 R2-D2      96    32 <NA>       white, bl… red             33   none          0
4 Darth…    202   136 none       white      yellow          41.9 male          1
5 Leia …    150    49 brown      light      brown           19   fema…         0

观察选添删

转置,汇总

Reference

{janitor} by Sam Firke

document by Albert Rapp: https://albert-rapp.de/posts/07_janitor_showcase/07_janitor_showcase.html https://www.youtube.com/watch?v=AKPvlNWZBEQ